<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="ActiveMQ, 鸟鸟">
    <meta name="description" content="大家好，我是鸟鸟，今年20岁，喜欢打篮球，搭建的这个博客是用来记录平时的一些学习笔记和心得">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>ActiveMQ | 鸟鸟</title>
    <link rel="icon" type="image/png" href="/yueyueniao-gif/favicon.png">

    <link rel="stylesheet" type="text/css" href="/yueyueniao-gif/libs/awesome/css/all.css">
    <link rel="stylesheet" type="text/css" href="/yueyueniao-gif/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/yueyueniao-gif/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/yueyueniao-gif/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/yueyueniao-gif/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/yueyueniao-gif/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/yueyueniao-gif/css/my.css">

    <script src="/yueyueniao-gif/libs/jquery/jquery.min.js"></script>

<meta name="generator" content="Hexo 5.0.2"><link rel="alternate" href="/yueyueniao-gif/atom.xml" title="鸟鸟" type="application/atom+xml">
<link rel="stylesheet" href="/yueyueniao-gif/css/prism-tomorrow.css" type="text/css"></head>


<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/yueyueniao-gif/" class="waves-effect waves-light">
                    
                    <img src="/yueyueniao-gif/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">鸟鸟</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/yueyueniao-gif/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/yueyueniao-gif/tags" class="waves-effect waves-light">
      
      <i class="fas fa-tags" style="zoom: 0.6;"></i>
      
      <span>标签</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/yueyueniao-gif/categories" class="waves-effect waves-light">
      
      <i class="fas fa-bookmark" style="zoom: 0.6;"></i>
      
      <span>分类</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/yueyueniao-gif/archives" class="waves-effect waves-light">
      
      <i class="fas fa-archive" style="zoom: 0.6;"></i>
      
      <span>归档</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/yueyueniao-gif/about" class="waves-effect waves-light">
      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/yueyueniao-gif/contact" class="waves-effect waves-light">
      
      <i class="fas fa-comments" style="zoom: 0.6;"></i>
      
      <span>留言板</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/yueyueniao-gif/friends" class="waves-effect waves-light">
      
      <i class="fas fa-address-book" style="zoom: 0.6;"></i>
      
      <span>友情链接</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>


<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/yueyueniao-gif/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">鸟鸟</div>
        <div class="logo-desc">
            
            大家好，我是鸟鸟，今年20岁，喜欢打篮球，搭建的这个博客是用来记录平时的一些学习笔记和心得
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/yueyueniao-gif/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/yueyueniao-gif/tags" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-tags"></i>
			
			标签
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/yueyueniao-gif/categories" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-bookmark"></i>
			
			分类
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/yueyueniao-gif/archives" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-archive"></i>
			
			归档
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/yueyueniao-gif/about" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-user-circle"></i>
			
			关于
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/yueyueniao-gif/contact" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-comments"></i>
			
			留言板
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/yueyueniao-gif/friends" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-address-book"></i>
			
			友情链接
		</a>
          
        </li>
        
        
        <li><div class="divider"></div></li>
        <li>
            <a href="https://github.com/blinkfox/hexo-theme-matery" class="waves-effect waves-light" target="_blank">
                <i class="fab fa-github-square fa-fw"></i>Fork Me
            </a>
        </li>
        
    </ul>
</div>


        </div>

        
            <style>
    .nav-transparent .github-corner {
        display: none !important;
    }

    .github-corner {
        position: absolute;
        z-index: 10;
        top: 0;
        right: 0;
        border: 0;
        transform: scale(1.1);
    }

    .github-corner svg {
        color: #0f9d58;
        fill: #fff;
        height: 64px;
        width: 64px;
    }

    .github-corner:hover .octo-arm {
        animation: a 0.56s ease-in-out;
    }

    .github-corner .octo-arm {
        animation: none;
    }

    @keyframes a {
        0%,
        to {
            transform: rotate(0);
        }
        20%,
        60% {
            transform: rotate(-25deg);
        }
        40%,
        80% {
            transform: rotate(10deg);
        }
    }
</style>

<a href="https://github.com/blinkfox/hexo-theme-matery" class="github-corner tooltipped hide-on-med-and-down" target="_blank"
   data-tooltip="Fork Me" data-position="left" data-delay="50">
    <svg viewBox="0 0 250 250" aria-hidden="true">
        <path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path>
        <path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2"
              fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path>
        <path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z"
              fill="currentColor" class="octo-body"></path>
    </svg>
</a>
        
    </nav>

</header>

    



<div class="bg-cover pd-header post-cover" style="background-image: url('/yueyueniao-gif/medias/featureimages/6.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">ActiveMQ</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/yueyueniao-gif/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        margin: 35px 0 15px 0;
        padding-left: 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        height: calc(100vh - 250px);
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;

        position: absolute;
        right: 23.5vw;
        display: block;
    }

    .toc-fixed .toc-link::before{
        position: fixed!important;/*当toc的位置改为fixed时，.toc-link::before也要改为fixed*/
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #toc-content .is-active-link::before {
        background-color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/yueyueniao-gif/tags/MQ/">
                                <span class="chip bg-color">MQ</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/yueyueniao-gif/categories/MQ/" class="post-category">
                                MQ
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2020-11-29
                </div>
                

                

                
                <div class="info-break-policy">
                    <i class="far fa-file-word fa-fw"></i>文章字数:&nbsp;&nbsp;
                    8.3k
                </div>
                

                

                
            </div>
        </div>
        <hr class="clearfix">
        <div class="card-content article-card-content">
            <div id="articleContent">
                <h1 id="ActiveMQ"><a href="#ActiveMQ" class="headerlink" title="ActiveMQ"></a>ActiveMQ</h1><h2 id="下载安装"><a href="#下载安装" class="headerlink" title="下载安装"></a>下载安装</h2><p><a target="_blank" rel="noopener" href="http://activemq.apache.org/">官网</a></p>
<p><img src="https://s3.jpg.cm/2020/11/29/sJC6R.png" alt="sJC6R.png"></p>
<ol>
<li><p>上传到liunx的opt目录下<img src="https://s3.jpg.cm/2020/11/29/sJKEz.png" alt="sJKEz.png"></p>
</li>
<li><p>然后使用tar -zxvf解压命令解压<img src="https://s3.jpg.cm/2020/11/29/sJhju.png" alt="sJhju.png"></p>
</li>
<li><p>然后在根目录下创建一个文件夹myactivemq，将解压的activemq移动到根目录下面<img src="https://s3.jpg.cm/2020/11/29/sJ9t4.png" alt="sJ9t4.png"></p>
</li>
<li><p>进入到/myactivemq/apache-activemq-5.16.0/bin目录下使用命令./activemq start启动activemq<img src="https://s3.jpg.cm/2020/11/29/sJT0X.png" alt="sJT0X.png"></p>
</li>
<li><p>使用命令查看ps -ef|grep activemq<img src="https://s3.jpg.cm/2020/11/29/sJiID.png" alt="sJiID.png"></p>
</li>
<li><p>activemq默认后端端口是61616，可以使用命令查看进程号 lsof -i:61616<img src="https://s3.jpg.cm/2020/11/29/sJxUp.png" alt="sJxUp.png"></p>
</li>
<li><p>activemq默认前端端口号是8161，在windows本机上打开<img src="https://s3.jpg.cm/2020/11/30/sJZ6E.png" alt="sJZ6E.png"></p>
</li>
<li><p>如果连接失败，检查防火墙和端口号是否打开，最后还是不行的话，</p>
<p>需要去修改下“/opt/myActiveMQ/apache-activemq-5.16.0/conf/jetty.xml”这个文件里的配置修改如下</p>
<pre class=" language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>bean</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>jettyPort<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>org.apache.activemq.web.WebConsolePort<span class="token punctuation">"</span></span> <span class="token attr-name">init-method</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>start<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
             <span class="token comment" spellcheck="true">&lt;!-- the default port number for the web console --></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>host<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>0.0.0.0<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>port<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>8161<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>bean</span><span class="token punctuation">></span></span></code></pre>
<p><img src="https://s3.jpg.cm/2020/11/29/sJDo6.png" alt="sJDo6.png"></p>
</li>
</ol>
<h2 id="入门ActiveMQ"><a href="#入门ActiveMQ" class="headerlink" title="入门ActiveMQ"></a>入门ActiveMQ</h2><h3 id="准备工作"><a href="#准备工作" class="headerlink" title="准备工作"></a>准备工作</h3><pre class=" language-xml"><code class="language-xml">   <span class="token comment" spellcheck="true">&lt;!-- activemq所需要的jar包配置 --></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.apache.activemq<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>activemq-all<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>5.16.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.apache.xbean<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>xbean-spring<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>3.16<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></code></pre>
<h3 id="消息生产者编码"><a href="#消息生产者编码" class="headerlink" title="消息生产者编码"></a>消息生产者编码</h3><ol>
<li><pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>atguigu<span class="token punctuation">.</span>activemq<span class="token punctuation">;</span>

<span class="token keyword">import</span> org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>activemq<span class="token punctuation">.</span>ActiveMQConnectionFactory<span class="token punctuation">;</span>

<span class="token keyword">import</span> javax<span class="token punctuation">.</span>jms<span class="token punctuation">.</span>*<span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">/**
 * @ClassName:JmsProduce
 * @Description: TODO
 * @author: wang yue niao
 * @date 2020/11/29 21:01
 * @Version 1.0
 */</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">JmsProduce</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span>  <span class="token keyword">final</span> String ACTIVEMQ_ERL<span class="token operator">=</span><span class="token string">"tcp://192.168.36.134:61616"</span><span class="token punctuation">;</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span>  <span class="token keyword">final</span> String QUEUE_NAME<span class="token operator">=</span><span class="token string">"queue01"</span><span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> JMSException <span class="token punctuation">{</span>
        <span class="token comment" spellcheck="true">//1.创建连接工场,按照给定的url地址，采用默认用户名和密码(admin,admin)</span>
        ActiveMQConnectionFactory am <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveMQConnectionFactory</span><span class="token punctuation">(</span>ACTIVEMQ_ERL<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//通过连接工场,获得connection并启动访问</span>
        Connection connection <span class="token operator">=</span> am<span class="token punctuation">.</span><span class="token function">createConnection</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        connection<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//创建会话session</span>
        <span class="token comment" spellcheck="true">//两个参数，第一个叫事务，第二个叫签收</span>
        Session session <span class="token operator">=</span> connection<span class="token punctuation">.</span><span class="token function">createSession</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">,</span>Session<span class="token punctuation">.</span>AUTO_ACKNOWLEDGE<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//创建目的地(具体是队列还是主题topic)</span>
        Queue queue <span class="token operator">=</span> session<span class="token punctuation">.</span><span class="token function">createQueue</span><span class="token punctuation">(</span>QUEUE_NAME<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//创建消息的生产者</span>
        MessageProducer messageProducer <span class="token operator">=</span> session<span class="token punctuation">.</span><span class="token function">createProducer</span><span class="token punctuation">(</span>queue<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//通过使用messageProducer产生三条小修发送到MQ的队列里面</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span><span class="token number">3</span><span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token comment" spellcheck="true">//创建消息</span>
            TextMessage textMessage <span class="token operator">=</span> session<span class="token punctuation">.</span><span class="token function">createTextMessage</span><span class="token punctuation">(</span><span class="token string">"msg---"</span> <span class="token operator">+</span> i<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment" spellcheck="true">//通过消息生产者MessageProducer发送给mq</span>
            messageProducer<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span>textMessage<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token comment" spellcheck="true">//关闭资源</span>
        messageProducer<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        session<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        connection<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"*****消息发发布到MQ完成"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
</li>
<li><p>查看<img src="https://s3.jpg.cm/2020/11/30/stGFQ.png" alt="stGFQ.png"><img src="https://s3.jpg.cm/2020/11/30/stnLH.png" alt="stnLH.png"></p>
</li>
<li><p>现在可以看到已经有三条消息发送过去了<img src="https://s3.jpg.cm/2020/11/30/stY7u.png" alt="stY7u.png"></p>
</li>
</ol>
<h3 id="消息消费者编码"><a href="#消息消费者编码" class="headerlink" title="消息消费者编码"></a>消息消费者编码</h3><p><strong>两种方式来创建消费者</strong></p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>atguigu<span class="token punctuation">.</span>activemq<span class="token punctuation">;</span>

<span class="token keyword">import</span> lombok<span class="token punctuation">.</span>SneakyThrows<span class="token punctuation">;</span>
<span class="token keyword">import</span> org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>activemq<span class="token punctuation">.</span>ActiveMQConnectionFactory<span class="token punctuation">;</span>

<span class="token keyword">import</span> javax<span class="token punctuation">.</span>jms<span class="token punctuation">.</span>*<span class="token punctuation">;</span>
<span class="token keyword">import</span> java<span class="token punctuation">.</span>io<span class="token punctuation">.</span>IOException<span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">/**
 * @ClassName:JmsConsumer
 * @Description: TODO
 * @author: wang yue niao
 * @date 2020/11/30 11:22
 * @Version 1.0
 */</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">JmsConsumer</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span>  <span class="token keyword">final</span> String ACTIVEMQ_ERL<span class="token operator">=</span><span class="token string">"tcp://192.168.36.134:61616"</span><span class="token punctuation">;</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span>  <span class="token keyword">final</span> String QUEUE_NAME<span class="token operator">=</span><span class="token string">"queue01"</span><span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> JMSException<span class="token punctuation">,</span> IOException <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
        System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"我是1号消费者"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//1.创建连接工场,按照给定的url地址，采用默认用户名和密码(admin,admin)</span>
        ActiveMQConnectionFactory am <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveMQConnectionFactory</span><span class="token punctuation">(</span>ACTIVEMQ_ERL<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//通过连接工场,获得connection并启动访问</span>
        Connection connection <span class="token operator">=</span> am<span class="token punctuation">.</span><span class="token function">createConnection</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        connection<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//创建会话session</span>
        <span class="token comment" spellcheck="true">//两个参数，第一个叫事务，第二个叫签收</span>
        Session session <span class="token operator">=</span> connection<span class="token punctuation">.</span><span class="token function">createSession</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">,</span>Session<span class="token punctuation">.</span>AUTO_ACKNOWLEDGE<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//创建目的地(具体是队列还是主题topic)</span>
        Queue queue <span class="token operator">=</span> session<span class="token punctuation">.</span><span class="token function">createQueue</span><span class="token punctuation">(</span>QUEUE_NAME<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//创建消费者</span>
        MessageConsumer consumer <span class="token operator">=</span> session<span class="token punctuation">.</span><span class="token function">createConsumer</span><span class="token punctuation">(</span>queue<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">/*
        同步阻塞方式(receive)
        订阅或接受者调用MessageConsumer的receive()方法来接受消息，receive方法在能够接受到消息之前(或超时之前)将一直阻塞
        while(true)&amp;#123;
            TextMessage message =(TextMessage)consumer.receive();
            if(null!=message)
            &amp;#123;
                System.out.println("****消费者接收到消息"+message.getText());
            &amp;#125;else&amp;#123;
                break;
            &amp;#125;
        &amp;#125;
        consumer.close();
        session.close();
        connection.close();
        */</span>
        <span class="token comment" spellcheck="true">//通过监听的方式来消费消息</span>
        consumer<span class="token punctuation">.</span><span class="token function">setMessageListener</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MessageListener</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
            <span class="token annotation punctuation">@SneakyThrows</span>
            <span class="token annotation punctuation">@Override</span>
            <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">onMessage</span><span class="token punctuation">(</span>Message message<span class="token punctuation">)</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
                <span class="token keyword">if</span><span class="token punctuation">(</span>null<span class="token operator">!=</span>message<span class="token operator">&amp;&amp;</span>message <span class="token keyword">instanceof</span> <span class="token class-name">TextMessage</span><span class="token punctuation">)</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
                    <span class="token keyword">try</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
                        TextMessage textMessage <span class="token operator">=</span> <span class="token punctuation">(</span>TextMessage<span class="token punctuation">)</span> message<span class="token punctuation">;</span>
                        System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"****消费者接收到消息"</span> <span class="token operator">+</span> textMessage<span class="token punctuation">.</span><span class="token function">getText</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span><span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">JMSException</span> e<span class="token punctuation">)</span><span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
                        e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
                <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
            <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
        <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        System<span class="token punctuation">.</span>in<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        consumer<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        session<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        connection<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
<span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
</code></pre>
<h3 id="JMS开发的基本步骤"><a href="#JMS开发的基本步骤" class="headerlink" title="JMS开发的基本步骤"></a>JMS开发的基本步骤</h3><p><img src="https://s3.jpg.cm/2020/11/30/sSGWS.png" alt="sSGWS.png"></p>
<ol>
<li>创建一个connection factory</li>
<li>通过connection factory来创建JMS connection</li>
<li>启动JMS connection</li>
<li>通过connection创建JMS session</li>
<li>创建JMS destination</li>
<li>创建JMS produce或者创建JMS message并设置destination</li>
<li>创建JMS consumer或者是注册一个JMS message listener</li>
<li>发送或者接受JMS message(s)</li>
<li>关闭所有的JMS资源(connection,session,producer,consumer等)</li>
</ol>
<h3 id="topic"><a href="#topic" class="headerlink" title="topic"></a>topic</h3><ol>
<li>生产者将消息发布到topic中，每个消息可以有多个消费者，属于1:N的关系</li>
<li>生产者和消费者之间时间上的相关性。订阅某一个主题的消费者只能消费自它订阅之后发布的消息</li>
<li>生产者生产时，topic不保存消息它时无状态的不落地，假如无人订阅就去生产，那就是一条废消息，所以，一般先启动消费者在启动生产者</li>
</ol>
<p><strong>生产者</strong></p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>atguigu<span class="token punctuation">.</span>activemq<span class="token punctuation">;</span>

<span class="token keyword">import</span> org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>activemq<span class="token punctuation">.</span>ActiveMQConnectionFactory<span class="token punctuation">;</span>

<span class="token keyword">import</span> javax<span class="token punctuation">.</span>jms<span class="token punctuation">.</span>*<span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">/**
 * @ClassName:JmsProduce_Topic
 * @Description: TODO
 * @author: wang yue niao
 * @date 2020/11/30     12:50
 * @Version 1.0
 */</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">JmsProduce_Topic</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span>  <span class="token keyword">final</span> String ACTIVEMQ_ERL<span class="token operator">=</span><span class="token string">"tcp://192.168.36.134:61616"</span><span class="token punctuation">;</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span>  <span class="token keyword">final</span> String TOPIC_NAME<span class="token operator">=</span><span class="token string">"topic01"</span><span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> JMSException <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//1.创建连接工场,按照给定的url地址，采用默认用户名和密码(admin,admin)</span>
        ActiveMQConnectionFactory am <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveMQConnectionFactory</span><span class="token punctuation">(</span>ACTIVEMQ_ERL<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//通过连接工场,获得connection并启动访问</span>
        Connection connection <span class="token operator">=</span> am<span class="token punctuation">.</span><span class="token function">createConnection</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        connection<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//创建会话session</span>
        <span class="token comment" spellcheck="true">//两个参数，第一个叫事务，第二个叫签收</span>
        Session session <span class="token operator">=</span> connection<span class="token punctuation">.</span><span class="token function">createSession</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">,</span>Session<span class="token punctuation">.</span>AUTO_ACKNOWLEDGE<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//创建目的地(具体是队列还是主题topic)</span>
        Topic  topic <span class="token operator">=</span> session<span class="token punctuation">.</span><span class="token function">createTopic</span><span class="token punctuation">(</span>TOPIC_NAME<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//创建消息的生产者</span>
        MessageProducer producer <span class="token operator">=</span> session<span class="token punctuation">.</span><span class="token function">createProducer</span><span class="token punctuation">(</span>topic<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//通过使用messageProducer产生三条小修发送到MQ的队列里面</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span><span class="token number">6</span><span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
            <span class="token comment" spellcheck="true">//创建消息</span>
            TextMessage textMessage <span class="token operator">=</span> session<span class="token punctuation">.</span><span class="token function">createTextMessage</span><span class="token punctuation">(</span><span class="token string">"msg---"</span> <span class="token operator">+</span> i<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment" spellcheck="true">//通过消息生产者MessageProducer发送给mq</span>
            producer<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span>textMessage<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//关闭资源</span>
        producer<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        session<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        connection<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"*****消息发发布到MQ完成"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
<span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
</code></pre>
<p><strong>消费者</strong></p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>atguigu<span class="token punctuation">.</span>activemq<span class="token punctuation">;</span>

<span class="token keyword">import</span> lombok<span class="token punctuation">.</span>SneakyThrows<span class="token punctuation">;</span>
<span class="token keyword">import</span> org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>activemq<span class="token punctuation">.</span>ActiveMQConnectionFactory<span class="token punctuation">;</span>

<span class="token keyword">import</span> javax<span class="token punctuation">.</span>jms<span class="token punctuation">.</span>*<span class="token punctuation">;</span>
<span class="token keyword">import</span> java<span class="token punctuation">.</span>io<span class="token punctuation">.</span>IOException<span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">/**
 * @ClassName:JmsConsumer_Topic
 * @Description: TODO
 * @author: wang yue niao
 * @date 2020/11/30 14:44
 * @Version 1.0
 */</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">JmsConsumer_Topic</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span>  <span class="token keyword">final</span> String ACTIVEMQ_ERL<span class="token operator">=</span><span class="token string">"tcp://192.168.36.134:61616"</span><span class="token punctuation">;</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span>  <span class="token keyword">final</span> String TOPIC_NAME<span class="token operator">=</span><span class="token string">"topic01"</span><span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> JMSException<span class="token punctuation">,</span> IOException <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
        System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"我是1号消费者"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//1.创建连接工场,按照给定的url地址，采用默认用户名和密码(admin,admin)</span>
        ActiveMQConnectionFactory am <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveMQConnectionFactory</span><span class="token punctuation">(</span>ACTIVEMQ_ERL<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//通过连接工场,获得connection并启动访问</span>
        Connection connection <span class="token operator">=</span> am<span class="token punctuation">.</span><span class="token function">createConnection</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        connection<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//创建会话session</span>
        <span class="token comment" spellcheck="true">//两个参数，第一个叫事务，第二个叫签收</span>
        Session session <span class="token operator">=</span> connection<span class="token punctuation">.</span><span class="token function">createSession</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">,</span>Session<span class="token punctuation">.</span>AUTO_ACKNOWLEDGE<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//创建目的地(具体是队列还是主题topic)</span>
        Topic topic <span class="token operator">=</span> session<span class="token punctuation">.</span><span class="token function">createTopic</span><span class="token punctuation">(</span>TOPIC_NAME<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//创建消费者</span>
        MessageConsumer consumer <span class="token operator">=</span> session<span class="token punctuation">.</span><span class="token function">createConsumer</span><span class="token punctuation">(</span>topic<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">/*
        同步阻塞方式(receive)
        订阅或接受者调用MessageConsumer的receive()方法来接受消息，receive方法在能够接受到消息之前(或超时之前)将一直阻塞
        while(true)&amp;#123;
            TextMessage message =(TextMessage)consumer.receive();
            if(null!=message)
            &amp;#123;
                System.out.println("****消费者接收到消息"+message.getText());
            &amp;#125;else&amp;#123;
                break;
            &amp;#125;
        &amp;#125;
        consumer.close();
        session.close();
        connection.close();
        */</span>
        <span class="token comment" spellcheck="true">//通过监听的方式来消费消息</span>
        consumer<span class="token punctuation">.</span><span class="token function">setMessageListener</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MessageListener</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
            <span class="token annotation punctuation">@SneakyThrows</span>
            <span class="token annotation punctuation">@Override</span>
            <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">onMessage</span><span class="token punctuation">(</span>Message message<span class="token punctuation">)</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
                <span class="token keyword">if</span><span class="token punctuation">(</span>null<span class="token operator">!=</span>message<span class="token operator">&amp;&amp;</span>message <span class="token keyword">instanceof</span> <span class="token class-name">TextMessage</span><span class="token punctuation">)</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
                    <span class="token keyword">try</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
                        TextMessage textMessage <span class="token operator">=</span> <span class="token punctuation">(</span>TextMessage<span class="token punctuation">)</span> message<span class="token punctuation">;</span>
                        System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"****消费者接收到消息"</span> <span class="token operator">+</span> textMessage<span class="token punctuation">.</span><span class="token function">getText</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span><span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">JMSException</span> e<span class="token punctuation">)</span><span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
                        e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
                <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
            <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
        <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        System<span class="token punctuation">.</span>in<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        consumer<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        session<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        connection<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
<span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span></code></pre>
<h3 id="topic和队列的总结"><a href="#topic和队列的总结" class="headerlink" title="topic和队列的总结"></a>topic和队列的总结</h3><table>
<thead>
<tr>
<th>比较项目</th>
<th>Topic模式队列</th>
<th>Queue模式队列</th>
</tr>
</thead>
<tbody><tr>
<td>工作模式</td>
<td>“订阅-发布”模式，如果当前没有订阅者，消息将会被丢弃，如果有多个订阅者，那么这些订阅者都会受到消息</td>
<td>“负载均衡”模式，如果当前没有消费者，消息也不会被丢弃；如果有多个消费者；那么一条消息也只会个发送给其中一个消费者，并且要求消费者ack消息</td>
</tr>
<tr>
<td>有无状态</td>
<td>无状态</td>
<td>Queue数据默认会在mq服务器上以文件形式保存</td>
</tr>
<tr>
<td>传递完整性</td>
<td>如果没有订阅者，消息会被丢弃</td>
<td>消息不会被丢弃</td>
</tr>
<tr>
<td>处理效率</td>
<td>由于消息要按照订阅者的数量进行复制，所以处理性能会随着订阅者的增加而明显降低，并且还要结合不同消息协议自身的性能差异</td>
<td>由于一条消息只发送给一个消费者，所以就算消费者再多，性能也不会有明显的降低，当然不同消息协议的具体性能也是有差异的</td>
</tr>
</tbody></table>
<h2 id="JMS"><a href="#JMS" class="headerlink" title="JMS"></a>JMS</h2><h3 id="什么是JMS"><a href="#什么是JMS" class="headerlink" title="什么是JMS"></a>什么是JMS</h3><p>Java Message Service（java消息服务是javaEE中的一个技术）</p>
<p>java消息服务是指两个应用程序之间进行异步通信的API，他为标准消息协议和消息服务提供了一组通用接口，包括创建，发送，读取消息等，用于支持JAVA应用程序开发。在JavaEE中，当两个应用程序使用JMS进行通信时，他们之间并不是直接相连的，而是通过一个公共的消息收发服务组件关联起来以达到解耦/异步削峰的效果。</p>
<h3 id="JMS的组成结构和特点"><a href="#JMS的组成结构和特点" class="headerlink" title="JMS的组成结构和特点"></a>JMS的组成结构和特点</h3><h4 id="JMS-provider"><a href="#JMS-provider" class="headerlink" title="JMS provider"></a>JMS provider</h4><ul>
<li>实现JMS接口和规范的消息中间件，也就是我们的MQ服务</li>
</ul>
<h4 id="JMS-prodeucer"><a href="#JMS-prodeucer" class="headerlink" title="JMS prodeucer"></a>JMS prodeucer</h4><ul>
<li>消息生产者，创建和发送JMS消息的客户端应用</li>
</ul>
<h4 id="JMS-consumer"><a href="#JMS-consumer" class="headerlink" title="JMS consumer"></a>JMS consumer</h4><ul>
<li>消息消费者，接收和处理JMS消息的客户端应用</li>
</ul>
<h4 id="JMS-message"><a href="#JMS-message" class="headerlink" title="JMS message"></a>JMS message</h4><h5 id="消息头"><a href="#消息头" class="headerlink" title="消息头"></a>消息头</h5><ul>
<li>JMSDestination ：消息发送的目的地，主要指Queue和Topic</li>
<li>JMSDdeliveryMode：持久和非持久模式<ul>
<li>一条持久性的消息：应该被传送“一次仅仅一次”，这就意味着如果JMS提供出现故障，该消息并不会丢失，他会在服务器恢复之后再次传递</li>
<li>一条非持久的消息：最多会传送一次，这意味着服务器出现故障，该消息将永远丢失</li>
</ul>
</li>
<li>JMSExpiration：消息的过期时间<ul>
<li>可以设置消息在一定时间后过期，默认是永不过期</li>
<li>消息过期时间，等与Destination的send方法中的timeToLive值加上发送时刻的GMT时间值</li>
<li>如果timeToLive值等与零，则JMSExpiration被设为零，表示该消息永不过期</li>
<li>如果发送后，在消息过期时间之后消息还没有被发送到目的地，则该消息被清除</li>
</ul>
</li>
<li>JMSPriority：消息优先级<ul>
<li>从0-9十个级别，0到4是普通消息，5-9是加急消息。</li>
<li>JMS不要求MQ严格按照这个十个优先级发送消息，但必须保证加急消息要先于普通消息到达。默认是4级</li>
</ul>
</li>
<li>JMSMessageID：唯一识别每个消息的标识由MQ产生</li>
</ul>
<h5 id="消息体"><a href="#消息体" class="headerlink" title="消息体"></a>消息体</h5><ul>
<li>封装具体的消息数据</li>
<li>5中消息体格式<ul>
<li>TextMessage：普通字符串消息，包含一个String</li>
<li>MapMessage：一个Map类型消息，key为String类型，而值为java的基本类型</li>
<li>BytesMessage：二进制数组消息，包含一个byte[]</li>
<li>StreamMessage：java数据流消息，用标准流操作来顺序的填充和读取</li>
<li>ObjectMessage：对象消息，包含一个可序列化的java对象</li>
</ul>
</li>
<li>发送和接收的消息体类型必须一致对应</li>
</ul>
<h5 id="消息属性"><a href="#消息属性" class="headerlink" title="消息属性"></a>消息属性</h5><ul>
<li>如果需要除消息头字段以外的值，那么可以使用消息属性</li>
<li>识别/去重/重点标注等操作非常有用的方法</li>
<li>他们是属性名和属性值对的形式制定的。可以将属性是为消息头得扩展，属性制定一些消息头没有包括的附加信息，比如可以在属性里指定消息选择器。</li>
<li>消息属性就像可以分配给一条消息的附加消息头一样。它们允许开发者添加有关消息的不透明附加信息。它们还用于暴露消息选择器在消息过滤时使用的数据。</li>
<li><img src="https://s3.jpg.cm/2020/11/30/srRTU.png" alt="srRTU.png"></li>
</ul>
<h3 id="JMS的可靠性"><a href="#JMS的可靠性" class="headerlink" title="JMS的可靠性"></a>JMS的可靠性</h3><h4 id="PERSISTENT：持久性"><a href="#PERSISTENT：持久性" class="headerlink" title="PERSISTENT：持久性"></a>PERSISTENT：持久性</h4><h5 id="参数说明"><a href="#参数说明" class="headerlink" title="参数说明"></a>参数说明</h5><ul>
<li><p>非持久</p>
<ul>
<li>messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT)</li>
<li>非持久化：当服务器宕机，消息不存在</li>
</ul>
</li>
<li><p>持久</p>
<ul>
<li>messageProducer.setDeliveryMode(DeliveryMode.PERSISENT)</li>
<li>持久化：当服务器宕机，消息依然存在</li>
</ul>
</li>
</ul>
<h5 id="持久的Queue"><a href="#持久的Queue" class="headerlink" title="持久的Queue"></a>持久的Queue</h5><ul>
<li>队列默认的是<strong>持久化消息</strong>传送模式，此模式保证这些消息只被传送一次和成功使用一次。</li>
</ul>
<h5 id="持久的Topic"><a href="#持久的Topic" class="headerlink" title="持久的Topic"></a>持久的Topic</h5><ul>
<li><p>效果：</p>
<ul>
<li>运行生产者，发布消息，多个消费者可以正常收到</li>
<li>关闭一个消费者，运行生产者，发布消息后再启动被关闭的消费者，可以收到离线后的消息；</li>
<li>关闭所有消费者，运行生产者，发布消息后，关闭ActiveMQ再启动，启动所有消费者，都可以收到消息。</li>
</ul>
</li>
<li><p>代码</p>
<ul>
<li><pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>atguigu<span class="token punctuation">.</span>activemq<span class="token punctuation">;</span>

<span class="token keyword">import</span> org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>activemq<span class="token punctuation">.</span>ActiveMQConnectionFactory<span class="token punctuation">;</span>

<span class="token keyword">import</span> javax<span class="token punctuation">.</span>jms<span class="token punctuation">.</span>*<span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">/**
 * @ClassName:JmsProduce_Topic
 * @Description: TODO
 * @author: wang yue niao
 * @date 2020/11/30     12:50
 * @Version 1.0
 */</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">JmsProduce_Topic</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span>  <span class="token keyword">final</span> String ACTIVEMQ_ERL<span class="token operator">=</span><span class="token string">"tcp://192.168.36.134:61616"</span><span class="token punctuation">;</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span>  <span class="token keyword">final</span> String TOPIC_NAME<span class="token operator">=</span><span class="token string">"topic01"</span><span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> JMSException <span class="token punctuation">{</span>
        ActiveMQConnectionFactory am <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveMQConnectionFactory</span><span class="token punctuation">(</span>ACTIVEMQ_ERL<span class="token punctuation">)</span><span class="token punctuation">;</span>
        Connection connection <span class="token operator">=</span> am<span class="token punctuation">.</span><span class="token function">createConnection</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        Session session <span class="token operator">=</span> connection<span class="token punctuation">.</span><span class="token function">createSession</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">,</span>Session<span class="token punctuation">.</span>AUTO_ACKNOWLEDGE<span class="token punctuation">)</span><span class="token punctuation">;</span>
        Topic  topic <span class="token operator">=</span> session<span class="token punctuation">.</span><span class="token function">createTopic</span><span class="token punctuation">(</span>TOPIC_NAME<span class="token punctuation">)</span><span class="token punctuation">;</span>
        MessageProducer producer <span class="token operator">=</span> session<span class="token punctuation">.</span><span class="token function">createProducer</span><span class="token punctuation">(</span>topic<span class="token punctuation">)</span><span class="token punctuation">;</span>
        producer<span class="token punctuation">.</span><span class="token function">setDeliveryMode</span><span class="token punctuation">(</span>DeliveryMode<span class="token punctuation">.</span>PERSISTENT<span class="token punctuation">)</span><span class="token punctuation">;</span>
        connection<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span><span class="token number">6</span><span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            TextMessage textMessage <span class="token operator">=</span> session<span class="token punctuation">.</span><span class="token function">createTextMessage</span><span class="token punctuation">(</span><span class="token string">"msg---"</span> <span class="token operator">+</span> i<span class="token punctuation">)</span><span class="token punctuation">;</span>
            producer<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span>textMessage<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        producer<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        session<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        connection<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"*****消息发发布到MQ完成"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
</li>
<li><pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>atguigu<span class="token punctuation">.</span>activemq<span class="token punctuation">;</span>

<span class="token keyword">import</span> lombok<span class="token punctuation">.</span>SneakyThrows<span class="token punctuation">;</span>
<span class="token keyword">import</span> org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>activemq<span class="token punctuation">.</span>ActiveMQConnectionFactory<span class="token punctuation">;</span>

<span class="token keyword">import</span> javax<span class="token punctuation">.</span>jms<span class="token punctuation">.</span>*<span class="token punctuation">;</span>
<span class="token keyword">import</span> java<span class="token punctuation">.</span>io<span class="token punctuation">.</span>IOException<span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">/**
 * @ClassName:JmsConsumer_Topic
 * @Description: TODO
 * @author: wang yue niao
 * @date 2020/11/30 14:44
 * @Version 1.0
 */</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">JmsConsumer_Topic</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span>  <span class="token keyword">final</span> String ACTIVEMQ_ERL<span class="token operator">=</span><span class="token string">"tcp://192.168.36.134:61616"</span><span class="token punctuation">;</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span>  <span class="token keyword">final</span> String TOPIC_NAME<span class="token operator">=</span><span class="token string">"topic01"</span><span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> JMSException<span class="token punctuation">,</span> IOException <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
        System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"我是zs消费者"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        ActiveMQConnectionFactory am <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveMQConnectionFactory</span><span class="token punctuation">(</span>ACTIVEMQ_ERL<span class="token punctuation">)</span><span class="token punctuation">;</span>
        Connection connection <span class="token operator">=</span> am<span class="token punctuation">.</span><span class="token function">createConnection</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        connection<span class="token punctuation">.</span><span class="token function">setClientID</span><span class="token punctuation">(</span><span class="token string">"zs"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        Session session <span class="token operator">=</span> connection<span class="token punctuation">.</span><span class="token function">createSession</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">,</span>Session<span class="token punctuation">.</span>AUTO_ACKNOWLEDGE<span class="token punctuation">)</span><span class="token punctuation">;</span>
        Topic topic <span class="token operator">=</span> session<span class="token punctuation">.</span><span class="token function">createTopic</span><span class="token punctuation">(</span>TOPIC_NAME<span class="token punctuation">)</span><span class="token punctuation">;</span>
        TopicSubscriber topicSubscriber <span class="token operator">=</span> session<span class="token punctuation">.</span><span class="token function">createDurableSubscriber</span><span class="token punctuation">(</span>topic<span class="token punctuation">,</span> <span class="token string">"remark.."</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        connection<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        Message message<span class="token operator">=</span>topicSubscriber<span class="token punctuation">.</span><span class="token function">receive</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">while</span> <span class="token punctuation">(</span>null<span class="token operator">!=</span>message<span class="token punctuation">)</span><span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
            TextMessage textMessage<span class="token operator">=</span><span class="token punctuation">(</span>TextMessage<span class="token punctuation">)</span>message<span class="token punctuation">;</span>
            System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"收到持久化topic："</span><span class="token operator">+</span>textMessage<span class="token punctuation">.</span><span class="token function">getText</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            message <span class="token operator">=</span> topicSubscriber<span class="token punctuation">.</span><span class="token function">receive</span><span class="token punctuation">(</span>1000L<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
        session<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        connection<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
<span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
</code></pre>
</li>
</ul>
</li>
</ul>
<h4 id="事务"><a href="#事务" class="headerlink" title="事务"></a>事务</h4><ul>
<li><p>false</p>
<ul>
<li>只要执行send，就进入到队列中。</li>
<li>关闭事务，那第二个签收参数的设置需要有效。</li>
</ul>
</li>
<li><p>true</p>
<ul>
<li><p>先执行send再执行commit，消息才被真正的提交到队列中。</p>
<pre class=" language-java"><code class="language-java">session<span class="token punctuation">.</span><span class="token function">commit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
</li>
</ul>
</li>
</ul>
<ul>
<li><p>消息需要批量发送，需要缓冲区处理。</p>
</li>
<li><p>如果一次提交十条消息，一条出错了，可以回滚</p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">try</span><span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">//ok</span>
<span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span><span class="token keyword">catch</span><span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
    session<span class="token punctuation">.</span><span class="token function">rollback</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span><span class="token keyword">finally</span><span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>null<span class="token operator">!</span>session<span class="token punctuation">)</span><span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
        session<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
<span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span></code></pre>
</li>
</ul>
<h4 id="Acknowledge：签收"><a href="#Acknowledge：签收" class="headerlink" title="Acknowledge：签收"></a>Acknowledge：签收</h4><ul>
<li>非事务<ul>
<li>自动签收（默认）Session.AUTO_ACKNOWLEDGE</li>
<li>手动签收 Session.CLIENT_ACKNOWLEDGE<ul>
<li>客户端调用acknowledge方法手动签收   message.acknowledge</li>
</ul>
</li>
<li>允许重复消息的签收 Session.DUPS_OK_ACKNOWLEDGE</li>
</ul>
</li>
<li>事务<ul>
<li>生产事务开启，只有commit后才能将全部消息变为已消费</li>
<li>事务开启，则默认为自动签收，无论第二个签收参数写什么都是相当于自动签收</li>
<li>如果开启事务，最后不session.commit的话会重复消费，而且消息不会被确认</li>
</ul>
</li>
<li>签收和事务的关系<ul>
<li>在事务会话中，当一个事务被成功提交则消息被自动签收。如果事务回滚，则消息会被再次传送。</li>
<li>非事务会话中，消息何时 被确认取决于创建会话时的应答模式是自动签收还是手动签收等等。</li>
</ul>
</li>
</ul>
<h4 id="JMS点对点总结"><a href="#JMS点对点总结" class="headerlink" title="JMS点对点总结"></a>JMS点对点总结</h4><ul>
<li>点对对模型是基于队列的，生产者发消息到队列，消费者从队列接收消息，队列的存在使得消息的异步传输成为可能。和我们平时给朋友发送短信类似。</li>
<li>如果在Session关闭时所有部分消息已被收到但还没有被签收，那消费者下次连接到相同的队列时，这些消息还会被再次接收。</li>
<li>队列可以长久地保存消息知道消费者收到消息。消费者不需要因为担心消息会丢失而时刻和队列保持激活的连接状态，充分体现了<strong>异步传输模式的优势</strong>。</li>
</ul>
<h4 id="JMS发布订阅总结"><a href="#JMS发布订阅总结" class="headerlink" title="JMS发布订阅总结"></a>JMS发布订阅总结</h4><ul>
<li>JMS Pub/Sub 模型定义了如何向一个内容节点发布和订阅消息，这些节点被称作topic。</li>
<li>主题可以被认为是消息的传输中介，发布者发布消息到主题，订阅者从主题订阅消息。</li>
<li>主题使得消息订阅者发布者保持回想独立，不需要解除即可保证消息的传送。</li>
</ul>
<h5 id="非持久订阅"><a href="#非持久订阅" class="headerlink" title="非持久订阅"></a>非持久订阅</h5><ul>
<li><p>非持久订阅只有当客户端处于激活状态，也就是和MQ保持连接状态才能收到发送到某个主题的消息。</p>
</li>
<li><p>如果消费者处于离线状态，生产者发送的主题消息将会丢失作废，消费者永远不会收到</p>
<p><strong>一句话：要先订阅注册才能接受到发布，只给订阅者发布消息</strong></p>
</li>
</ul>
<h5 id="持久订阅"><a href="#持久订阅" class="headerlink" title="持久订阅"></a>持久订阅</h5><ul>
<li>客户端首先向MQ注册一个自己的身份ID识别号，当这个客户端处于离线时，生产者会为这个ID保存所有发送到主 题的消息，当客户再次连接到MQ时会根据消费者的ID得到所有当自己离线时发送到主题的消息。</li>
<li>非持久订阅状态下，不能回复或重新派送一个未签收的消息。</li>
<li>持久订阅才能回复或重新派送一个未签收的消息。</li>
</ul>
<h5 id="用哪个？"><a href="#用哪个？" class="headerlink" title="用哪个？"></a>用哪个？</h5><ul>
<li>当所有消息必须被接受，则用持久订阅。当丢失消息能够被容忍，则用非持久订阅。</li>
</ul>
<h2 id="SpringBoot整合ActiveMQ"><a href="#SpringBoot整合ActiveMQ" class="headerlink" title="SpringBoot整合ActiveMQ"></a>SpringBoot整合ActiveMQ</h2><h3 id="队列生产者"><a href="#队列生产者" class="headerlink" title="队列生产者"></a>队列生产者</h3><h4 id="1-新建maven工程并设置包名类名"><a href="#1-新建maven工程并设置包名类名" class="headerlink" title="1.新建maven工程并设置包名类名"></a>1.新建maven工程并设置包名类名</h4><ul>
<li>工程名  boot_mq_produce</li>
<li>包名  com.atguigu.boot.activemq</li>
</ul>
<h4 id="2-POM-xml"><a href="#2-POM-xml" class="headerlink" title="2.POM.xml"></a>2.POM.xml</h4><pre class=" language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>parent</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-starter-parent<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>2.1.5.RELEASE<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>relativePath</span><span class="token punctuation">/></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>parent</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependencies</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-starter<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-starter-web<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-starter-test<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-starter-activemq<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>2.1.5.RELEASE<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependencies</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>build</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>plugins</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>plugin</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-maven-plugin<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>plugin</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>plugins</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>build</span><span class="token punctuation">></span></span></code></pre>
<h4 id="3-application-yml"><a href="#3-application-yml" class="headerlink" title="3.application.yml"></a>3.application.yml</h4><pre class=" language-yml"><code class="language-yml">server:
  port: 8080
spring:
  activemq:
    broker-url: tcp://192.169.36.134:61616 #自己的mq服务器地址
    user: admin
    password: admin
  jms:
    pub-sub-domain: false    #false=Queue    true=Topic



#自己定义队列名称
myqueue: boot-activemq-queue

</code></pre>
<h4 id="4-配置Bean"><a href="#4-配置Bean" class="headerlink" title="4.配置Bean"></a>4.配置Bean</h4><pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@Component</span>
<span class="token annotation punctuation">@EnableJms</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ConfingBean</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
    <span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">"$&amp;#123;myqueue&amp;#125;"</span><span class="token punctuation">)</span>
    <span class="token keyword">private</span> String myQueue<span class="token punctuation">;</span>
    <span class="token annotation punctuation">@Bean</span>
    <span class="token keyword">public</span> Queue <span class="token function">queue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">ActiveMQQueue</span><span class="token punctuation">(</span>myQueue<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
<span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span></code></pre>
<h4 id="5-Queue-Produce"><a href="#5-Queue-Produce" class="headerlink" title="5.Queue_Produce"></a>5.Queue_Produce</h4><pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@Component</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Queue_Produce</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> JmsMessagingTemplate jmsMessagingTemplate<span class="token punctuation">;</span>
    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> Queue queue<span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">prodeuceMsg</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
        jmsMessagingTemplate<span class="token punctuation">.</span><span class="token function">convertAndSend</span><span class="token punctuation">(</span>queue<span class="token punctuation">,</span><span class="token string">"*******"</span><span class="token operator">+</span> UUID<span class="token punctuation">.</span><span class="token function">randomUUID</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
<span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span></code></pre>
<h4 id="6-主启动类MainApp-Prodeuce"><a href="#6-主启动类MainApp-Prodeuce" class="headerlink" title="6.主启动类MainApp_Prodeuce"></a>6.主启动类MainApp_Prodeuce</h4><pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@SpringBootApplication</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MainApp_Prodeuce</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
        SpringApplication<span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span>MainApp_Prodeuce<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span>args<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
<span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
</code></pre>
<h4 id="7-单元测试"><a href="#7-单元测试" class="headerlink" title="7.单元测试"></a>7.单元测试</h4><pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@SpringBootTest</span><span class="token punctuation">(</span>classes <span class="token operator">=</span> MainApp_Prodeuce<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span>
<span class="token annotation punctuation">@RunWith</span><span class="token punctuation">(</span>SpringJUnit4ClassRunner<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span>
<span class="token annotation punctuation">@WebAppConfiguration</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TestActivemq</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
    <span class="token annotation punctuation">@Resource</span>
    <span class="token keyword">private</span> Queue_Produce queue_produce<span class="token punctuation">;</span>

    <span class="token annotation punctuation">@Test</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testsend</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token keyword">throws</span>  Exception<span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
        queue_produce<span class="token punctuation">.</span><span class="token function">prodeuceMsg</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
<span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
</code></pre>
<h3 id="队列消费者"><a href="#队列消费者" class="headerlink" title="队列消费者"></a>队列消费者</h3><h4 id="1-新建maven工程并设置报名类名"><a href="#1-新建maven工程并设置报名类名" class="headerlink" title="1.新建maven工程并设置报名类名"></a>1.新建maven工程并设置报名类名</h4><ul>
<li>工程名  boot_mq_consumer</li>
<li>包名   com.atguigu.boot.activemq</li>
</ul>
<h4 id="2-POM-xml-1"><a href="#2-POM-xml-1" class="headerlink" title="2.POM.xml"></a>2.POM.xml</h4><pre class=" language-xml"><code class="language-xml"><span class="token prolog">&lt;?xml version="1.0" encoding="UTF-8"?></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>project</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://maven.apache.org/POM/4.0.0<span class="token punctuation">"</span></span>
         <span class="token attr-name"><span class="token namespace">xmlns:</span>xsi</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://www.w3.org/2001/XMLSchema-instance<span class="token punctuation">"</span></span>
         <span class="token attr-name"><span class="token namespace">xsi:</span>schemaLocation</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>modelVersion</span><span class="token punctuation">></span></span>4.0.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>modelVersion</span><span class="token punctuation">></span></span>

    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>com.wangyueniao<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>boot_mq_consumer<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>1.0-SNAPSHOT<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>

    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>parent</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-starter-parent<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>2.1.5.RELEASE<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>relativePath</span><span class="token punctuation">/></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>parent</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependencies</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-starter<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-starter-web<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-starter-test<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-starter-activemq<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>2.1.5.RELEASE<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependencies</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>build</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>plugins</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>plugin</span><span class="token punctuation">></span></span>
                <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
                <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-maven-plugin<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>plugin</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>plugins</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>build</span><span class="token punctuation">></span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>project</span><span class="token punctuation">></span></span>
</code></pre>
<h4 id="3-application-yml-1"><a href="#3-application-yml-1" class="headerlink" title="3.application.yml"></a>3.application.yml</h4><pre class=" language-yml"><code class="language-yml">server:
  port: 8081

spring:
  activemq:
    broker-url: tcp://192.168.36.134:61616 #自己的mq服务器地址
    user: admin
    password: admin
  jms:
    pub-sub-domain: false    #false=Queue    true=Topic



#自己定义队列名称
myqueue: boot-activemq-queue

</code></pre>
<h4 id="4-Queue-Consumer"><a href="#4-Queue-Consumer" class="headerlink" title="4.Queue_Consumer"></a>4.Queue_Consumer</h4><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Queue_Consumer</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
    <span class="token annotation punctuation">@JmsListener</span><span class="token punctuation">(</span>destination <span class="token operator">=</span> <span class="token string">"$&amp;#123;myqueue&amp;#125;"</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">receive</span><span class="token punctuation">(</span>TextMessage textMessage<span class="token punctuation">)</span><span class="token keyword">throws</span> Exception<span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
        System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"******消费者收到消息"</span><span class="token operator">+</span>textMessage<span class="token punctuation">.</span><span class="token function">getText</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
<span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
</code></pre>
<h4 id="5-主启动类"><a href="#5-主启动类" class="headerlink" title="5.主启动类"></a>5.主启动类</h4><pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@SpringBootApplication</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MainApp_Consumer</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
        SpringApplication<span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span>MainApp_Consumer<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span>args<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
<span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
</code></pre>
<h3 id="Topic生产者"><a href="#Topic生产者" class="headerlink" title="Topic生产者"></a>Topic生产者</h3><h4 id="1-新建maven工程斌设置包名和类名"><a href="#1-新建maven工程斌设置包名和类名" class="headerlink" title="1.新建maven工程斌设置包名和类名"></a>1.新建maven工程斌设置包名和类名</h4><ul>
<li>工程名  boot_mq_topic_produce</li>
<li>包名   com.atguigu.boot.activemq.topic.produce</li>
</ul>
<h4 id="2-POM-xml-2"><a href="#2-POM-xml-2" class="headerlink" title="2.POM.xml"></a>2.POM.xml</h4><pre class=" language-xml"><code class="language-xml"><span class="token prolog">&lt;?xml version="1.0" encoding="UTF-8"?></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>project</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://maven.apache.org/POM/4.0.0<span class="token punctuation">"</span></span>
         <span class="token attr-name"><span class="token namespace">xmlns:</span>xsi</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://www.w3.org/2001/XMLSchema-instance<span class="token punctuation">"</span></span>
         <span class="token attr-name"><span class="token namespace">xsi:</span>schemaLocation</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>modelVersion</span><span class="token punctuation">></span></span>4.0.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>modelVersion</span><span class="token punctuation">></span></span>

    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>com.wangyueniao<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>boot_mq_topic_produce<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>1.0-SNAPSHOT<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>parent</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-starter-parent<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>2.1.5.RELEASE<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>relativePath</span><span class="token punctuation">/></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>parent</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependencies</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-starter<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-starter-web<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-starter-test<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-starter-activemq<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>2.1.5.RELEASE<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependencies</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>build</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>plugins</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>plugin</span><span class="token punctuation">></span></span>
                <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
                <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-maven-plugin<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>plugin</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>plugins</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>build</span><span class="token punctuation">></span></span>


<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>project</span><span class="token punctuation">></span></span>
</code></pre>
<h4 id="3-application-yml-2"><a href="#3-application-yml-2" class="headerlink" title="3.application.yml"></a>3.application.yml</h4><pre class=" language-yml"><code class="language-yml">server:
  port: 8080

spring:
  activemq:
    broker-url: tcp://192.168.36.134:61616    #自己服务器地址
    user: admin
    password: admin
  jms:
    pub-sub-domain: true   #值表示topic


#自己定义topic的名字
myTopic: boot-activemq-topic
</code></pre>
<h4 id="4-配置Bean-1"><a href="#4-配置Bean-1" class="headerlink" title="4.配置Bean"></a>4.配置Bean</h4><pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@Component</span>
<span class="token annotation punctuation">@EnableJms</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ConfigBean</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
    <span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">"myTopic"</span><span class="token punctuation">)</span>
    <span class="token keyword">private</span> String topicName<span class="token punctuation">;</span>
    <span class="token annotation punctuation">@Bean</span>
    <span class="token keyword">public</span> Topic <span class="token function">topic</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">ActiveMQTopic</span><span class="token punctuation">(</span>topicName<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
<span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
</code></pre>
<h4 id="5-Topic-Produce"><a href="#5-Topic-Produce" class="headerlink" title="5.Topic_Produce"></a>5.Topic_Produce</h4><pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@Component</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Topic_Produce</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> JmsMessagingTemplate jmsMessagingTemplate<span class="token punctuation">;</span>
    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> Topic topic<span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">produceTopic</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
        jmsMessagingTemplate<span class="token punctuation">.</span><span class="token function">convertAndSend</span><span class="token punctuation">(</span>topic<span class="token punctuation">,</span><span class="token string">"主题消息：123456"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
<span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span></code></pre>
<h3 id="Topic消费者"><a href="#Topic消费者" class="headerlink" title="Topic消费者"></a>Topic消费者</h3><h4 id="1-新建maven工程并设置包名类名-1"><a href="#1-新建maven工程并设置包名类名-1" class="headerlink" title="1.新建maven工程并设置包名类名"></a>1.新建maven工程并设置包名类名</h4><ul>
<li>工程名   boot_mq_topic_consumer</li>
<li>包名   com.atguigu.boot.activemq.topic.consumer</li>
</ul>
<h4 id="2-POM-xml-3"><a href="#2-POM-xml-3" class="headerlink" title="2.POM.xml"></a>2.POM.xml</h4><pre class=" language-xml"><code class="language-xml">    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>parent</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-starter-parent<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>2.1.5.RELEASE<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>relativePath</span><span class="token punctuation">/></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>parent</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependencies</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-starter<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-starter-web<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-starter-test<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-starter-activemq<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>2.1.5.RELEASE<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependencies</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>build</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>plugins</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>plugin</span><span class="token punctuation">></span></span>
                <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
                <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-maven-plugin<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>plugin</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>plugins</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>build</span><span class="token punctuation">></span></span></code></pre>
<h4 id="3-application-yml-3"><a href="#3-application-yml-3" class="headerlink" title="3.application.yml"></a>3.application.yml</h4><pre class=" language-yml"><code class="language-yml">server:
  port: 8082

spring:
  activemq:
    broker-url: tcp://192.168.36.134:61616    #自己服务器地址
    user: admin
    password: admin
  jms:
    pub-sub-domain: true   #值表示topic


#自己定义topic的名字
myTopic: boot-activemq-topic
</code></pre>
<h4 id="4-Topic-Consumer"><a href="#4-Topic-Consumer" class="headerlink" title="4.Topic_Consumer"></a>4.Topic_Consumer</h4><pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@Component</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Topic_Consumer</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
    <span class="token annotation punctuation">@JmsListener</span><span class="token punctuation">(</span>destination <span class="token operator">=</span> <span class="token string">"$&amp;#123;myTopic&amp;#125;"</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">receive</span><span class="token punctuation">(</span>TextMessage text<span class="token punctuation">)</span><span class="token keyword">throws</span> JMSException<span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
        <span class="token keyword">try</span><span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
            System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"消费者收到订阅的主题："</span><span class="token operator">+</span>text<span class="token punctuation">.</span><span class="token function">getText</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span><span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span><span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
            e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
    <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
<span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
</code></pre>
<h2 id="ActiveMQ的传输协议"><a href="#ActiveMQ的传输协议" class="headerlink" title="ActiveMQ的传输协议"></a>ActiveMQ的传输协议</h2><h3 id="面试题"><a href="#面试题" class="headerlink" title="面试题"></a>面试题</h3><ul>
<li><p>默认61616端口号如何修改</p>
</li>
<li><p>你生产上的链接协议如何配置？使用tcp吗？</p>
</li>
</ul>
<h3 id="官网"><a href="#官网" class="headerlink" title="官网"></a>官网</h3><p><a target="_blank" rel="noopener" href="http://activemq.apache.org/configuring-version-5-transports.html">http://activemq.apache.org/configuring-version-5-transports.html</a></p>
<h3 id="是什么"><a href="#是什么" class="headerlink" title="是什么"></a>是什么</h3><ul>
<li>打开conf目录下的activemq.xml可以看到如下<ul>
<li><img src="https://s3.jpg.cm/2020/12/02/EGug6.png" alt="EGug6.png"></li>
<li>在上文给出的配置信息中，URL描述信息的头部都是采用协议名称：例如，<ul>
<li>描述amqp协议的监听端口时，采用的是URL描述格式为“amqp://……”</li>
<li>描述Stomp协议的监听端口时，采用的是URL描述格式为“stomp://……”</li>
<li>唯独在进行openwire协议描述时，URL头却采用的“tcp://……”，这是因为ActiveMQ中默认协议就是openwire。</li>
</ul>
</li>
</ul>
</li>
</ul>
<h3 id="有哪些"><a href="#有哪些" class="headerlink" title="有哪些"></a>有哪些</h3><ul>
<li>Transmission Control Protocol(TCP)我是默认<ul>
<li>这是默认的Broker配置，TCP的Client监听端口61616</li>
<li>在网络传输数据前，必须要序列化数据，消息是通过一个叫wire protocol的来序列化成字节流。默认情况下ActiveMQ把wire protocol叫做OpenWire，它的目的是促使网络上的效率和数据快速交互。</li>
<li>TCP连接的RUL形式如下：tcp://hostname:port?key=value&amp;key=value,后面参数是可选</li>
<li>TCP传输的优点：<ul>
<li>传输可靠性强，稳定性强</li>
<li>高效性：字节流方式传递，效率很高</li>
<li>有效性、可用性：应用广泛，支持任何平台</li>
</ul>
</li>
</ul>
</li>
<li>New I/O API Protocol(NIO)<ul>
<li>NIO协议和TCP协议类似但NIO更侧重于底层的访问操作。它允许开发人员对同一资源有更多的client调用和服务端与更多负载。</li>
<li>适合使用NIO协议的场景：<ul>
<li>可能有大量的client去连接到Broker，一般情况下，大量的Client去连接Broker是被操作系统线程限制的，因此，NIO的实现比TCP需要更少的线程去运行，所以建议使用NIO协议</li>
<li>可能对于Broker有一个儿很吃顿的网络传输，NIO比TCP提供更好的性能。</li>
</ul>
</li>
<li>NIO连接的URL形式：nio//hostname:port?key=value</li>
</ul>
</li>
<li>AMQP协议</li>
<li>stomp协议</li>
<li>Secure Sockerts Layer Protocol(SSL)</li>
<li>mqtt协议</li>
<li>ws协议</li>
<li>小总结</li>
</ul>
<h3 id="NIO案例演示"><a href="#NIO案例演示" class="headerlink" title="NIO案例演示"></a>NIO案例演示</h3><pre class=" language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>transportConnectors</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>transportConnector</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>nio<span class="token punctuation">"</span></span> <span class="token attr-name">uri</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>nio://0.0.0.0:61618?trace<span class="token punctuation">=</span>true<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>transportConnectors</span><span class="token punctuation">></span></span>


如果你不特别的指定ActiveMQ的网络监听端口，那么这些端口豆浆使用BIO网络IO模型。（OpenWire，STOMP，AMQP....）
所以为了首先提高单节点的网络吞吐性能，我们需要明确指定Active的网络IO模型，
如下图所示：URL格式头以"nio"，表示这个端口使用以TCP协议为基础的NIO网络IO模型。</code></pre>
<p><img src="https://s3.jpg.cm/2020/12/02/EGxh4.png" alt="EGxh4.png"></p>
<ul>
<li>代码如下，其实就是将tcp改成nio，并且端口配置到nio的端口</li>
</ul>
<pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>atguigu<span class="token punctuation">.</span>activemq<span class="token punctuation">;</span>

<span class="token keyword">import</span> org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>activemq<span class="token punctuation">.</span>ActiveMQConnectionFactory<span class="token punctuation">;</span>

<span class="token keyword">import</span> javax<span class="token punctuation">.</span>jms<span class="token punctuation">.</span>*<span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">/**
 * @ClassName:JmsProduce
 * @Description: TODO
 * @author: wang yue niao
 * @date 2020/11/29 21:01
 * @Version 1.0
 */</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">JmsProduce</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span>  <span class="token keyword">final</span> String ACTIVEMQ_ERL<span class="token operator">=</span><span class="token string">"nio://192.168.36.134:61618"</span><span class="token punctuation">;</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span>  <span class="token keyword">final</span> String QUEUE_NAME<span class="token operator">=</span><span class="token string">"transport"</span><span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> JMSException <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//1.创建连接工场,按照给定的url地址，采用默认用户名和密码(admin,admin)</span>
        ActiveMQConnectionFactory am <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveMQConnectionFactory</span><span class="token punctuation">(</span>ACTIVEMQ_ERL<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//通过连接工场,获得connection并启动访问</span>
        Connection connection <span class="token operator">=</span> am<span class="token punctuation">.</span><span class="token function">createConnection</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        connection<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//创建会话session</span>
        <span class="token comment" spellcheck="true">//两个参数，第一个叫事务，第二个叫签收</span>
        Session session <span class="token operator">=</span> connection<span class="token punctuation">.</span><span class="token function">createSession</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">,</span>Session<span class="token punctuation">.</span>AUTO_ACKNOWLEDGE<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//创建目的地(具体是队列还是主题topic)</span>
        Queue queue <span class="token operator">=</span> session<span class="token punctuation">.</span><span class="token function">createQueue</span><span class="token punctuation">(</span>QUEUE_NAME<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//创建消息的生产者</span>
        MessageProducer producer <span class="token operator">=</span> session<span class="token punctuation">.</span><span class="token function">createProducer</span><span class="token punctuation">(</span>queue<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//通过使用messageProducer产生三条小修发送到MQ的队列里面</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span><span class="token number">4</span><span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
            <span class="token comment" spellcheck="true">//创建消息</span>
            TextMessage textMessage <span class="token operator">=</span> session<span class="token punctuation">.</span><span class="token function">createTextMessage</span><span class="token punctuation">(</span><span class="token string">"msg---"</span> <span class="token operator">+</span> i<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment" spellcheck="true">//通过消息生产者MessageProducer发送给mq</span>
            producer<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span>textMessage<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//关闭资源</span>
        producer<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        session<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        connection<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"*****消息发发布到MQ完成"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
<span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span></code></pre>
<h3 id="NIO案例演示增强"><a href="#NIO案例演示增强" class="headerlink" title="NIO案例演示增强"></a>NIO案例演示增强</h3><ul>
<li>问题<ul>
<li>URL格式以”nio”开头，表示端口使用以TCP协议为基础的NIO网络IO模型。但是这样的设置方式，只能使这个端口支持Openwire协议。</li>
</ul>
</li>
<li>怎么既然这个端口支持NIO网络IO模型，又让它支持多个协议呢？</li>
<li>解决<ul>
<li>使用auto关键字</li>
<li>使用”+”符号来为端口设置多种特性</li>
<li>如果即需要某一个端口支持NIO网络IO模型，又需要它支持多个协议</li>
</ul>
</li>
</ul>
<h2 id="ActiveMQ的消息存储和持久化"><a href="#ActiveMQ的消息存储和持久化" class="headerlink" title="ActiveMQ的消息存储和持久化"></a>ActiveMQ的消息存储和持久化</h2><ul>
<li>为了避免意外的宕机后丢失信息，需要做到重启后可以回复消息队列，消息系统一般都会采用持久化机制。</li>
<li>ActiveMQ的消息持久化机制有JDBC,AMQ,KahaDB,和LevelDB，无论使用哪种持久化方式，消息的存储逻辑都是一致的。</li>
<li>就是在发送者将消息发送出去后，消息中心首先将消息存储到本地数据文件，内存数据库或者远程数据库等再视图将消息发送给接收者，成功则将消息从存储中删除，失败则继续尝试发送</li>
<li>消息中心启动后，首先要检查指定的存储位置，如果有未发送成功的消息，则需要把消息发送出去。</li>
</ul>
<h3 id="KahaDB消息存储（默认）"><a href="#KahaDB消息存储（默认）" class="headerlink" title="KahaDB消息存储（默认）"></a>KahaDB消息存储（默认）</h3><ul>
<li>基于日志文件，从ActiveMQ5.4开始默认的持久化插件、</li>
<li>说明<ul>
<li>KahaDB时目前默认的存储方式，可以用于任何场景，提高了性能和回复能力。</li>
<li>消息存储使用一个十五日志和仅仅用一个索引文件来存储它所有地址</li>
<li>KahaDB是一个专门针对消息持久化的解决方案，它对典型的消息使用模式进行了优化</li>
<li>数据被追加到data logs中。当不需要log文件中的数据的时候，log文件会被丢弃</li>
<li><a target="_blank" rel="noopener" href="https://imagelol.com/image/ELDfp"><img src="https://s3.jpg.cm/2020/12/03/ELDfp.md.png" alt="ELDfp.md.png"></a></li>
</ul>
</li>
<li>KahaDB的存储原理<ul>
<li>KahaDB在消息保存目录中只有4类文件和一个lock，跟ActiveMQ的其它几种文件存储引擎相比这就非常简洁了</li>
<li><img src="https://s3.jpg.cm/2020/12/03/ELMZ6.png" alt="ELMZ6.png"></li>
<li><img src="https://s3.jpg.cm/2020/12/03/EPGpE.png" alt="EPGpE.png"></li>
<li><img src="https://s3.jpg.cm/2020/12/03/EPPdh.png" alt="EPPdh.png"></li>
<li><img src="https://s3.jpg.cm/2020/12/03/EPqKW.png" alt="EPqKW.png"></li>
<li><img src="https://s3.jpg.cm/2020/12/03/EPRrS.png" alt="EPRrS.png"></li>
</ul>
</li>
<li><a target="_blank" rel="noopener" href="http://activemq.apache.org/persistence.html">官网传送门</a></li>
</ul>
<h3 id="JDBC消息存储"><a href="#JDBC消息存储" class="headerlink" title="JDBC消息存储"></a>JDBC消息存储</h3><ul>
<li><p><strong>1. 将数据库的驱动jar包和你想使用的数据库连接池的jar包复制到/myactivemq/apache-activemq-5.16.0/lib目录下，这里默认使用的是dbcp数据库连接池</strong></p>
</li>
<li><p><strong>2. 修改activemq.xml文件如下</strong></p>
</li>
</ul>
<p><img src="https://s3.jpg.cm/2020/12/03/EWZ0W.png" alt="EWZ0W.png"></p>
<ul>
<li><strong>3. 数据库连接池-配置（放在之外和<import>里面，也就是他俩之间）</import></strong></li>
</ul>
<p><img src="C:\Users\LENOVO\AppData\Roaming\Typora\typora-user-images\image-20201203162642153.png" alt="image-20201203162642153"></p>
<ul>
<li><p><strong>4. 建一个名为activemq的数据库</strong></p>
<ul>
<li><p>三张表的说明</p>
<ul>
<li><p>AACTIVEMQ_MSGS</p>
<p><img src="https://s3.jpg.cm/2020/12/03/EnRxf.png" alt="EnRxf.png"></p>
</li>
<li><p>ACTIVEMQ_ACKS</p>
<p><img src="https://s3.jpg.cm/2020/12/03/EnnEO.png" alt="EnnEO.png"></p>
</li>
<li><p>ACTIVEMQ_LOCK</p>
<p><img src="https://s3.jpg.cm/2020/12/03/En5jw.png" alt="En5jw.png"></p>
</li>
</ul>
</li>
</ul>
</li>
<li><p>小总结</p>
<ul>
<li>如果是queue，在没有消费者消费的情况下会将消息保存到activemq_msgs表中，只要有任意一个消费者消费过了，在消费之后这些消息将会立即被删除。</li>
<li>如果是topic，一般是先启动消费订阅然后再生产的情况下会将消息保存到activemq_acks。</li>
</ul>
</li>
<li><p>开发中的坑</p>
<ul>
<li>记得需要使用到的相关jar文件放置到ActiveMQ安装路径下的lib目录下。mysql-jdbc驱动的jar包和对应的数据库连接池jar包。</li>
<li>在jdbcPersistenceAdapte标签中设置了createTablesOnStartup属性为true时在第一次启动ActiveMQ时，ActiveMQ服务节点会自动创建所需要的数据表。启动完成后可以去掉这个属性，或者更改create TablesOnStartup属性为false。</li>
<li>“java.lang.l llegalStateException:BeanFactory not initialized or already closed”这是因为操作系统的机器名中有” _ “符号。更改机器名重启即可解决问题。</li>
</ul>
</li>
</ul>
<h3 id="JDBC-Message-store-with-ActiveMQ-Journal"><a href="#JDBC-Message-store-with-ActiveMQ-Journal" class="headerlink" title="JDBC Message store with ActiveMQ Journal"></a>JDBC Message store with ActiveMQ Journal</h3><ul>
<li>这种方式克服了JDBC Store的不足，JDBC每次消息过来，都需要去写库和读库。</li>
<li>ActiveMQ Journal，使用高速缓存写入技术，大大提高了性能。</li>
<li>当消费者的消费速度能够及时跟上生产者消息的生产速度时，Journal文件能够大大减少需要写入到DB中的消息。</li>
<li>举个例子：生产者生产了1000条消息，这1000条消息会保存到journal文件，如果消费者的消费速度很快的情况下，在journal文件还没有同步到DB之前，消费者已经消费了90%的以上消息，那么这个时候只需要同步剩余的10%的消息到DB。</li>
<li>如果消费者的消费速度很慢，这个时候journal文件可以使消息以批量方式写到DB。</li>
<li>配置<ul>
<li><img src="https://s3.jpg.cm/2020/12/03/EnBb2.png" alt="EnBb2.png"></li>
</ul>
</li>
</ul>
<h3 id="LevelDB消息存储（了解）"><a href="#LevelDB消息存储（了解）" class="headerlink" title="LevelDB消息存储（了解）"></a>LevelDB消息存储（了解）</h3><ul>
<li><p>这种文件系统是从ActiveMQ5.8之后引进的，他和KahaDB非常相似，也是基于文件的本地数据库储存形式，但是他提供给比KahaDB更快的持久性</p>
</li>
<li><p>但它不使用自定义B-Tree实现来索引预写文件，而是使用基于LevalDB的索引</p>
</li>
<li><p>默认配置如下:</p>
<ul>
<li><pre class=" language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>persistenceAdapter</span><span class="token punctuation">></span></span>
       &lt;levelDBdirectory="activemq-data"/>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>persistenceAdapter</span><span class="token punctuation">></span></span></code></pre>
</li>
</ul>
</li>
</ul>
<h2 id="ActiveMQ多节点集群"><a href="#ActiveMQ多节点集群" class="headerlink" title="ActiveMQ多节点集群"></a>ActiveMQ多节点集群</h2><h3 id="ZK-Replicated-LevelDB-Store"><a href="#ZK-Replicated-LevelDB-Store" class="headerlink" title="ZK+Replicated LevelDB Store"></a>ZK+Replicated LevelDB Store</h3><ul>
<li><p>原理说明</p>
<ul>
<li>使用ZooKeeper集群注册所有的ActiveMQ Broker但只有其中的一个Broker可以提供服务它将被视为Mater，其他的Broker处于待机状态被视为Slave。</li>
<li>如果Master因故障而不能提供服务ZooKeeper会从中选举一个Broker充当Master。</li>
<li>Slave连接Master并同步他们的存储状态，Slave不接受客户端连接。所有的存储操作都将被复制到连接至Master的Slaves。</li>
<li>如果Master宕机得到了最新更新的Slave会成为Master。故障节点在恢复后会重新加入到集群中并连接Master进入Slave模式</li>
<li>所有需要同步的消息操作都将等待存储状态被复制到其他法定节点的操作完成才能完成。</li>
<li>所以，如果你配置了replicas=3，那么法定大小时（3/2）+1=2.Master将会存储并更新然后等待（2-1）=1个Slave存储和更新完成，才汇报success。</li>
<li>有一个node要作为观察者存在。当一个新的Master被选中，你需要至少保障一个法定node在线以能够找到拥有最新状态的node。这个node才可以成为新的Master。</li>
</ul>
</li>
<li><p>部署规划和步骤</p>
<ul>
<li><p>创建三台集群目录<img src="https://s3.jpg.cm/2020/12/03/En9Pk.png" alt="En9Pk.png"></p>
</li>
<li><p>修改管理控制台端口</p>
<ul>
<li><p>mq_node01全部默认不动</p>
</li>
<li><p>mq_node02</p>
<p><a target="_blank" rel="noopener" href="https://imagelol.com/image/EnTue"><img src="https://s3.jpg.cm/2020/12/03/EnTue.md.png" alt="EnTue.md.png"></a></p>
</li>
<li><p>mq_node03</p>
<p><img src="https://s3.jpg.cm/2020/12/03/Ena4y.png" alt="Ena4y.png"></p>
</li>
</ul>
</li>
<li><p>hostname名字映射</p>
<ul>
<li><img src="https://s3.jpg.cm/2020/12/03/EniMr.png" alt="EniMr.png"></li>
</ul>
</li>
<li><p>AvtiveMQ集群配置</p>
<ul>
<li>三个节点的brokerName都要一样</li>
<li><img src="https://s3.jpg.cm/2020/12/03/EQe0h.png" alt="EQe0h.png"></li>
<li><img src="C:\Users\LENOVO\AppData\Roaming\Typora\typora-user-images\image-20201203192544716.png" alt="image-20201203192544716"></li>
<li>修改持久化配置</li>
<li><img src="C:\Users\LENOVO\AppData\Roaming\Typora\typora-user-images\image-20201203192940914.png" alt="image-20201203192940914"></li>
</ul>
</li>
<li><p>修改各节点的消息端口</p>
<ul>
<li>mq_node01默认不动</li>
<li><img src="https://s3.jpg.cm/2020/12/03/EQVbW.png" alt="EQVbW.png"></li>
</ul>
</li>
<li><p>按顺序启动3个ActiveMQ节点，到这步前提是zk集群已经成功启动</p>
</li>
</ul>
</li>
</ul>
<h2 id="高级特性和大厂常考重点"><a href="#高级特性和大厂常考重点" class="headerlink" title="高级特性和大厂常考重点"></a>高级特性和大厂常考重点</h2><h3 id="引入消息队列之后该如何保证其高可用性"><a href="#引入消息队列之后该如何保证其高可用性" class="headerlink" title="引入消息队列之后该如何保证其高可用性"></a>引入消息队列之后该如何保证其高可用性</h3><ul>
<li>zookeeper+replicated-leveldb-store</li>
</ul>
<h3 id="异步投递Async-Sends"><a href="#异步投递Async-Sends" class="headerlink" title="异步投递Async Sends"></a>异步投递Async Sends</h3><p><img src="https://s3.jpg.cm/2020/12/03/EQbyU.png" alt="EQbyU.png"></p>
<p><img src="https://s3.jpg.cm/2020/12/03/EQJ28.png" alt="EQJ28.png"></p>
<h3 id="延迟投递和定时投递"><a href="#延迟投递和定时投递" class="headerlink" title="延迟投递和定时投递"></a>延迟投递和定时投递</h3><h3 id="分发策略"><a href="#分发策略" class="headerlink" title="分发策略"></a>分发策略</h3><h3 id="ActiveMQ消费重试机制"><a href="#ActiveMQ消费重试机制" class="headerlink" title="ActiveMQ消费重试机制"></a>ActiveMQ消费重试机制</h3><h3 id="死信队列"><a href="#死信队列" class="headerlink" title="死信队列"></a>死信队列</h3><h3 id="如何保证消息不被重复消费呢？幂等性问题你谈谈"><a href="#如何保证消息不被重复消费呢？幂等性问题你谈谈" class="headerlink" title="如何保证消息不被重复消费呢？幂等性问题你谈谈"></a>如何保证消息不被重复消费呢？幂等性问题你谈谈</h3><script>
        document.querySelectorAll('.github-emoji')
          .forEach(el => {
            if (!el.dataset.src) { return; }
            const img = document.createElement('img');
            img.style = 'display:none !important;';
            img.src = el.dataset.src;
            img.addEventListener('error', () => {
              img.remove();
              el.style.color = 'inherit';
              el.style.backgroundImage = 'none';
              el.style.background = 'none';
            });
            img.addEventListener('load', () => {
              img.remove();
            });
            document.body.appendChild(img);
          });
      </script>
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://gitee.com/yueyueniao-gif/yueyueniao-gif.git" rel="external nofollow noreferrer">Wang Yue Niao</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://gitee.com/yueyueniao-gif/yueyueniao-gif.git/yueyueniao-gif/2020/11/29/activemq/">https://gitee.com/yueyueniao-gif/yueyueniao-gif.git/yueyueniao-gif/2020/11/29/activemq/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="https://gitee.com/yueyueniao-gif/yueyueniao-gif.git" target="_blank">Wang Yue Niao</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/yueyueniao-gif/tags/MQ/">
                                    <span class="chip bg-color">MQ</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/yueyueniao-gif/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="twitter,facebook,google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/yueyueniao-gif/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.4rem;
        line-height: 38px;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="/yueyueniao-gif/medias/reward/alipay.jpg" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="/yueyueniao-gif/medias/reward/wechat.png" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>

            
        </div>
    </div>

    

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/yueyueniao-gif/2020/12/06/linux/">
                    <div class="card-image">
                        
                        
                        <img src="/yueyueniao-gif/medias/featureimages/12.jpg" class="responsive-img" alt="Linux">
                        
                        <span class="card-title">Linux</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            Linuxlinux目录管理Linux的目录中有且只有一个根目录
Linux的各个目录存放的内容是规划好，不用乱放文件
Linux是以文件形式管理我们的设备，因此linux系统，一切皆文件

/bin：bin 是 Binaries (二进制
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2020-12-06
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/yueyueniao-gif/categories/Linux/" class="post-category">
                                    Linux
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/yueyueniao-gif/tags/Linux/">
                        <span class="chip bg-color">Linux</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/yueyueniao-gif/2020/11/17/shu-ju-jie-gou/">
                    <div class="card-image">
                        
                        
                        <img src="/yueyueniao-gif/medias/featureimages/7.jpg" class="responsive-img" alt="数据结构">
                        
                        <span class="card-title">数据结构</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            数据结构树B-树概念

每个节点最多有m个分支（子树）；而最少分支数要看是否为根节点，如果是根节点且不是叶子节点则至少有2个分支，非根叶节点至少有m/2（向上取正）个分支。
有n（k&lt;=n&lt;=m）个分支的节点有n-1个关键字，它
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2020-11-17
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/yueyueniao-gif/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/" class="post-category">
                                    数据结构
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/yueyueniao-gif/tags/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/">
                        <span class="chip bg-color">数据结构</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/yueyueniao-gif/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/yueyueniao-gif/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/yueyueniao-gif/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/yueyueniao-gif/libs/codeBlock/codeShrink.js"></script>


<!-- 代码块折行 -->

<style type="text/css">
code[class*="language-"], pre[class*="language-"] { white-space: pre !important; }
</style>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/yueyueniao-gif/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h2, h3, h4'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h2, h3, h4').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>


<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=default"></script>

    <footer class="page-footer bg-color">
    
        <link rel="stylesheet" href="/yueyueniao-gif/libs/aplayer/APlayer.min.css">
<style>
    .aplayer .aplayer-lrc p {
        font-size: 12px;
        font-weight: 700;
        line-height: 16px !important;
    }

    .aplayer .aplayer-lrc p.aplayer-lrc-current {
        font-size: 15px;
        color: #42b983;
    }

    
    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body {
        left: -66px !important;
    }

    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body:hover {
        left: 0px !important;
    }

    
</style>
<div class="">
    
    <div class="row">
        <meting-js class="col l8 offset-l2 m10 offset-m1 s12"
                   server="netease"
                   type="playlist"
                   id="2566555574"
                   fixed='true'
                   autoplay='false'
                   theme='#42b983'
                   loop='all'
                   order='random'
                   preload='auto'
                   volume='0.7'
                   list-folded='true'
        >
        </meting-js>
    </div>
</div>

<script src="/yueyueniao-gif/libs/aplayer/APlayer.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/meting@2/dist/Meting.min.js"></script>

    
    <div class="container row center-align" style="margin-bottom: 0px !important;">
       <h2>没有任何动物比蚂蚁更勤奋，然而它却最沉默寡言</h2><br><br>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script src="/yueyueniao-gif/js/search.js"></script>
<script type="text/javascript">
$(function () {
    searchFunc("/yueyueniao-gif/search.xml", 'searchInput', 'searchResult');
});
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/yueyueniao-gif/libs/materialize/materialize.min.js"></script>
    <script src="/yueyueniao-gif/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/yueyueniao-gif/libs/aos/aos.js"></script>
    <script src="/yueyueniao-gif/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/yueyueniao-gif/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/yueyueniao-gif/js/matery.js"></script>

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/yueyueniao-gif/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="/yueyueniao-gif/libs/others/busuanzi.pure.mini.js"></script>
    

    

    

    

    

    

    
    <script src="/yueyueniao-gif/libs/instantpage/instantpage.js" type="module"></script>
    

</body>

</html>
